//
//  WBNanigationController.swift
//  传智微博
//
//  Created by Pro on 2017/8/1.
//  Copyright © 2017年 liuyanwei. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController {

    override func viewDidLoad() {
        navigationBar.isHidden = true //可以手势返回
       /// self.setNavigationBarHidden(true, animated: true) // 不能手势返回
    }
    
    //重写push 方法，所有的push动作都会调用次方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
//        如果不是栈底控制器才需要隐藏，根控制器不需要处理
        if childViewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            if let vc = viewController as? WBBaseViewController {
                var title = "返回"
                //判断控制器的级数
                if childViewControllers.count == 1 {
                    title = childViewControllers.first?.title ?? "返回"
                }
                //取出自定义的navItem
                vc.navItem.leftBarButtonItem = UIBarButtonItem.init(title: title, target: self, action: #selector(popToParent),isBack:true)
            }
        }
        
        super.pushViewController(viewController, animated: true)
    }
    
    @objc fileprivate func popToParent() {
        popViewController(animated: true)
    }
}
